package com.maotai.llzhou.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpOptions;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.maotai.llzhou.bean.CustomName;
import com.maotai.llzhou.bean.MaotaiWebCookie;

public class HttpClientUtilNew {
	private static HttpClientUtilNew instance = null;
	
	private HttpClientUtilNew() {
	}

	public static HttpClientUtilNew getInstance() {
		if (instance == null) {
			instance = new HttpClientUtilNew();
		}
		return instance;
	}
	public MaotaiWebCookie sendHttpGetProxyIndex(String httpUrl,MaotaiWebCookie mwc, RequestConfig config,String referer) {
		HttpGet httpGet = new HttpGet(httpUrl);
		httpGet = setHttpGetHeaderIndex(httpGet,mwc,referer);
		return sendHttpGetProxyIndex(httpGet,mwc,config);
	}
	public MaotaiWebCookie sendHttpGetDataProxy(String httpUrl,MaotaiWebCookie mwc, RequestConfig config,String referer) throws Exception {
		HttpGet httpGet = new HttpGet(httpUrl);
		httpGet = setHttpGetDataHeader(httpGet,mwc,referer);
		return sendHttpGetProxy(httpGet,mwc,config);
	}
	public MaotaiWebCookie sendHttpGetProxy(String httpUrl,MaotaiWebCookie mwc, RequestConfig config,String referer) throws Exception {
		HttpGet httpGet = new HttpGet(httpUrl);
		httpGet = setHttpGetHeader(httpGet,mwc,referer);
		return sendHttpGetProxy(httpGet,mwc,config);
	}
	public MaotaiWebCookie sendHttpGetAppProxy(String httpUrl,MaotaiWebCookie mwc, RequestConfig config,String referer) throws Exception {
		HttpGet httpGet = new HttpGet(httpUrl);
		httpGet = setHttpGetAppHeader(httpGet,mwc,referer);
		return sendHttpGetProxy(httpGet,mwc,config);
	}
	/**
	 * 
	 * @param httpUrl
	 * @throws IOException 
	 * @throws ParseException 
	 */
	private MaotaiWebCookie sendHttpGetProxy(HttpGet httpGet,MaotaiWebCookie mwc, RequestConfig config) throws Exception {
		CloseableHttpClient httpClient = null;
		CloseableHttpResponse response = null;
		HttpEntity entity = null;
		String return_str = null;
		httpClient = HttpClients.createDefault();
		
		httpGet.setConfig(config);
		response = httpClient.execute(httpGet);
		Header[] headers = response.getAllHeaders();
		if(headers!=null&&headers.length>0){
        	for (Header hea : headers) {
//            		System.out.println(hea.getName() + "::::::::::::::"
//							+ hea.getValue());
				if(hea.getName().contains("auth")){
					mwc.setAuth(hea.getValue());
				}
				if(hea.getName().contains("Set-Cookie")||hea.getName().contains("set-cookie")){
					String temp = hea.getValue();
					if(temp.contains("acw_tc")){
						mwc.setAcw_tc(temp.split(";")[0].split("=")[1]);
					}
					if(temp.contains("csrfToken")){
						mwc.setCsrfToken(temp.split(";")[0].split("=")[1]);
					}
				}
			}
        }
		entity = response.getEntity();
		return_str = EntityUtils.toString(entity, "UTF-8");
		mwc.setContent(return_str);
		//System.out.println(return_str);
	
		try {
			if (response != null) {
				response.close();
			}
			if (httpClient != null) {
				httpClient.close();
			}
		} catch (Exception e) {
//				e.printStackTrace();
		}

		return mwc;
	}
	private MaotaiWebCookie sendHttpGetProxyIndex(HttpGet httpGet,MaotaiWebCookie mwc, RequestConfig config) {
		CloseableHttpClient httpClient = null;
		CloseableHttpResponse response = null;
		HttpEntity entity = null;
		String return_str = null;
		try {
			httpClient = HttpClients.createDefault();
			
			httpGet.setConfig(config);
			response = httpClient.execute(httpGet);
			Header[] headers = response.getAllHeaders();
			if(headers!=null&&headers.length>0){
            	for (Header hea : headers) {
//            		System.out.println(hea.getName() + "::::::::::::::"
//							+ hea.getValue());
					if(hea.getName().contains("auth")){
						mwc.setAuth(hea.getValue());
					}
					if(hea.getName().contains("Set-Cookie")||hea.getName().contains("set-cookie")){
						String temp = hea.getValue();
						if(temp.contains("acw_tc")){
							mwc.setAcw_tc(temp.split(";")[0].split("=")[1]);
						}
						if(temp.contains("csrfToken")){
							mwc.setCsrfToken(temp.split(";")[0].split("=")[1]);
						}
					}
				}
            }
			entity = response.getEntity();
			return_str = EntityUtils.toString(entity, "UTF-8");
			mwc.setContent(return_str);
//			System.out.println(return_str);
		} catch (Exception e) {
//			e.printStackTrace();
		} finally {
			try {
				if (response != null) {
					response.close();
				}
				if (httpClient != null) {
					httpClient.close();
				}
			} catch (Exception e) {
//				e.printStackTrace();
			}
		}
		return mwc;
	}
	
	private HttpGet setHttpGetDataHeader(HttpGet httpGet,MaotaiWebCookie mwc,String referer) {
		httpGet.addHeader("Accept",
				"image/webp,image/apng,image/*,*/*;q=0.8");
		httpGet.addHeader("Accept-Encoding", "gzip, deflate, br");
		httpGet.addHeader("Accept-Language", "zh-CN,zh;q=0.9");
		httpGet.addHeader("Connection", "keep-alive");
		httpGet.addHeader("Host", "i.emaotai.cn");
		httpGet.addHeader("Referer", referer);
		httpGet.addHeader(
				"User-Agent",
				"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36");
		
		String cookie = "";
		if(mwc.getAcw_tc()!=null && !"".equals(mwc.getAcw_tc())){
			cookie += "acw_tc"+mwc.getAcw_tc()+";";
		}
		if(mwc.getKcross()!=null && !"".equals(mwc.getKcross())){
			cookie += "sensorsdata2015jssdkcross="+mwc.getKcross()+";";
			cookie += "sajssdk_2015_cross_new_user=1;";
		}
		if( !"".equals(cookie)){
			httpGet.addHeader("Cookie",cookie);
		}
		return httpGet;
	}
	private HttpGet setHttpGetHeaderIndex(HttpGet httpGet,MaotaiWebCookie mwc,String referer) {
		httpGet.addHeader("Accept",
				"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
		httpGet.addHeader("Accept-Encoding", "gzip, deflate, br");
		httpGet.addHeader("Accept-Language", "zh-CN,zh;q=0.8");
		httpGet.addHeader("Connection", "keep-alive");
		httpGet.addHeader("Host", "www.emaotai.cn");
		if(!"".equals(referer)){
			httpGet.addHeader("Referer", referer);
		}
		
		httpGet.addHeader("Content-type", "application/x-www-form-urlencoded");
		httpGet.addHeader(
				"User-Agent",
				"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36");
		
		String cookie = "";
//		if(mwc.getAcw_tc()!=null && !"".equals(mwc.getAcw_tc())&&!"null".equals(mwc.getAcw_tc())){
//			cookie += "acw_tc"+mwc.getAcw_tc()+";";
//		}
		if(mwc.getCsrfToken()!=null && !"".equals(mwc.getCsrfToken())&&!"null".equals(mwc.getCsrfToken())){
			cookie += "csrfToken"+mwc.getCsrfToken()+";";
		}
		if(mwc.getAuth()!=null && !"".equals(mwc.getAuth())&&!"null".equals(mwc.getAuth())){
			cookie += "sso.login.account.auth="+mwc.getAuth()+";";
		}
		if(mwc.getKcross()!=null && !"".equals(mwc.getKcross())&&!"null".equals(mwc.getKcross())){
			cookie += "sensorsdata2015jssdkcross="+mwc.getKcross()+";";
			cookie += "sajssdk_2015_cross_new_user=1;";
		}
		if( !"".equals(cookie)){
			httpGet.addHeader("Cookie",cookie);
		}
		return httpGet;
	}
	
	private HttpGet setHttpGetAppHeader(HttpGet httpGet,MaotaiWebCookie mwc,String referer) throws Exception {
		httpGet.addHeader("Accept","application/json");
		httpGet.addHeader("appcode","2");
		httpGet.addHeader("Accept-Encoding", "");
		httpGet.addHeader("Connection", "keep-alive");
		httpGet.addHeader("Host", "i.cmaotai.com");
		httpGet.addHeader("tenantid", "1");
		httpGet.addHeader("channelCode", "02");
		httpGet.addHeader("terminaltype", "d1");
		httpGet.addHeader("Content-Type","application/json;charset=UTF-8");
		httpGet.addHeader("User-Agent","okhttp/3.6.0");
		httpGet.addHeader("reqid",System.currentTimeMillis()+"");
		httpGet.addHeader("terminaltype", "d1");
		
		if(mwc.getAuth()!=null && !"".equals(mwc.getAuth())){
			httpGet.addHeader("auth",mwc.getAuth());
		}
		return httpGet;
	}
	private HttpGet setHttpGetHeader(HttpGet httpGet,MaotaiWebCookie mwc,String referer) throws Exception {
		httpGet.addHeader("Accept",
				"application/json, text/javascript, */*; q=0.01");
		httpGet.addHeader("Accept-Encoding", "gzip, deflate, br");
		httpGet.addHeader("Accept-Language", "zh-CN,zh;q=0.8");
		httpGet.addHeader("Connection", "keep-alive");
		httpGet.addHeader("Host", "i.emaotai.cn");
		httpGet.addHeader("Referer", referer);
		httpGet.addHeader("Content-type", "application/x-www-form-urlencoded");
		httpGet.addHeader("appId", "1");
		httpGet.addHeader("channelId", "01");
		httpGet.addHeader("tenantId", "1");
		httpGet.addHeader("terminalType", "a1");
		httpGet.addHeader("Flag", "1");
		httpGet.addHeader("Timestamp", System.currentTimeMillis()+"");
		httpGet.addHeader("channelCode", "01");
		httpGet.addHeader(
				"User-Agent",
				"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36");
		if(mwc.getAuth()!=null && !"".equals(mwc.getAuth())){
			httpGet.addHeader("auth",mwc.getAuth());
			String timestamp = System.currentTimeMillis()+"";
			String sign = JSUtil.excuFunctionA(mwc.getAuth()+timestamp);
			httpGet.addHeader("Sign", sign);
			httpGet.addHeader("Timestamp", timestamp);
			
		}
//		String cookie = "";
//		if(mwc.getAcw_tc()!=null && !"".equals(mwc.getAcw_tc())){
//			cookie += "acw_tc"+mwc.getAcw_tc()+";";
//		}
//		if(mwc.getCsrfToken()!=null && !"".equals(mwc.getCsrfToken())){
//			cookie += "csrfToken"+mwc.getCsrfToken()+";";
//		}
//		if(mwc.getAuth()!=null && !"".equals(mwc.getAuth())){
//			cookie += "sso.login.account.auth="+mwc.getAuth()+";";
//		}
//		if(mwc.getKcross()!=null && !"".equals(mwc.getKcross())){
//			cookie += "sensorsdata2015jssdkcross="+mwc.getKcross()+";";
//		}
//		if( !"".equals(cookie)){
//			httpGet.addHeader("Cookie",cookie);
//		}
		return httpGet;
	}
	
	private HttpOptions setHttpOptionsHeader(HttpOptions httpPost, MaotaiWebCookie mwc ,String referer) throws Exception {
		httpPost.addHeader("Accept","application/json, text/javascript, */*; q=0.01");
		httpPost.addHeader("Accept-Encoding", "gzip, deflate, br");
		httpPost.addHeader("Accept-Language", "zh-CN,zh;q=0.9");
		httpPost.addHeader("Connection", "keep-alive");
		httpPost.addHeader("Host", "i.emaotai.cn");
		httpPost.addHeader("Content-Type","application/x-www-form-urlencoded");
		httpPost.addHeader("Origin","https://www.emaotai.cn");
		httpPost.addHeader("Referer",referer);
		httpPost.addHeader("channelCode","01");
		httpPost.addHeader("terminalType","a1");
		httpPost.addHeader("appId", "1");
		httpPost.addHeader("channelId", "01");
		httpPost.addHeader("tenantId", "1");
		httpPost.addHeader("Flag", "1");
		httpPost.addHeader(
				"User-Agent",
				"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36");
		if(mwc.getAuth()!=null && !"".equals(mwc.getAuth())){
			httpPost.addHeader("auth",mwc.getAuth());
			String timestamp = System.currentTimeMillis()+"";
			String sign = JSUtil.excuFunctionA(mwc.getAuth()+timestamp);
			httpPost.addHeader("Sign", sign);
			httpPost.addHeader("Timestamp", timestamp);
		}
//		String cookie = "";
//		if(mwc.getAcw_tc()!=null && !"".equals(mwc.getAcw_tc())){
//			cookie += "acw_tc"+mwc.getAcw_tc()+";";
//		}
//		if(mwc.getCsrfToken()!=null && !"".equals(mwc.getCsrfToken())){
//			cookie += "csrfToken"+mwc.getCsrfToken()+";";
//		}
//		if(mwc.getAuth()!=null && !"".equals(mwc.getAuth())){
//			cookie += "sso.login.account.auth="+mwc.getAuth()+";";
//		}
//		if(mwc.getKcross()!=null && !"".equals(mwc.getKcross())){
//			cookie += "sensorsdata2015jssdkcross="+mwc.getKcross()+";";
//		}
//		if( !"".equals(cookie)){
//			httpPost.addHeader("Cookie",cookie);
//		}
		return httpPost;
	}
	
	private HttpPost setHttpPostLoginHeader(HttpPost httpPost, MaotaiWebCookie mwc ,String referer) throws Exception {
		httpPost.addHeader("Accept","application/json, text/javascript, */*; q=0.01");
		httpPost.addHeader("Accept-Encoding", "gzip, deflate, br");
		httpPost.addHeader("Accept-Language", "zh-CN,zh;q=0.9");
		httpPost.addHeader("Connection", "keep-alive");
		httpPost.addHeader("Host", "www.emaotai.cn");
		httpPost.addHeader("Content-Type","application/x-www-form-urlencoded");
		httpPost.addHeader("Origin","https://www.emaotai.cn");
		httpPost.addHeader("Referer",referer);
		httpPost.addHeader("channelCode","01");
		httpPost.addHeader("terminalType","a1");
		httpPost.addHeader("appId", "1");
		httpPost.addHeader("channelId", "01");
		httpPost.addHeader("tenantId", "1");
		httpPost.addHeader("Flag", "1");
		httpPost.addHeader(
				"User-Agent",
				"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36");
		if(mwc.getAuth()!=null && !"".equals(mwc.getAuth())){
			httpPost.addHeader("auth",mwc.getAuth());
			String timestamp = System.currentTimeMillis()+"";
			String sign = JSUtil.excuFunctionA(mwc.getAuth()+timestamp);
			httpPost.addHeader("Sign", sign);
			httpPost.addHeader("Timestamp", timestamp);
		}
		String cookie = "";
//		if(mwc.getAcw_tc()!=null && !"".equals(mwc.getAcw_tc())){
//			cookie += "acw_tc"+mwc.getAcw_tc()+";";
//		}
		if(mwc.getCsrfToken()!=null && !"".equals(mwc.getCsrfToken())){
			cookie += "csrfToken"+mwc.getCsrfToken()+";";
		}
		if(mwc.getAuth()!=null && !"".equals(mwc.getAuth())){
			cookie += "sso.login.account.auth="+mwc.getAuth()+";";
		}
		if(mwc.getKcross()!=null && !"".equals(mwc.getKcross())){
			cookie += "sensorsdata2015jssdkcross="+mwc.getKcross()+";";
			cookie += "sajssdk_2015_cross_new_user=1;";
		}
		if( !"".equals(cookie)){
			httpPost.addHeader("Cookie",cookie);
		}
		return httpPost;
	}
	
	private HttpPost setHttpPostAppHeader(HttpPost httpPost, MaotaiWebCookie mwc ) throws Exception {
		httpPost.addHeader("reqid",""+System.currentTimeMillis());
		httpPost.addHeader("Accept","application/json");
		httpPost.addHeader("Accept-Encoding", "");
		httpPost.addHeader("appcode", "2");
		httpPost.addHeader("Connection", "keep-alive");
		httpPost.addHeader("Host", "i.cmaotai.com");
		httpPost.addHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
		httpPost.addHeader("channelcode","02");
		httpPost.addHeader("terminaltype","d1");
		httpPost.addHeader("tenantid", "1");
		httpPost.addHeader("User-Agent","okhttp/3.6.0");
		if(mwc.getAuth()!=null && !"".equals(mwc.getAuth())){
			httpPost.addHeader("auth",mwc.getAuth());
		}
		return httpPost;
	}
	private HttpPost setHttpPostHeader(HttpPost httpPost, MaotaiWebCookie mwc ,String referer) throws Exception {
		httpPost.addHeader("Accept","application/json, text/javascript, */*; q=0.01");
		httpPost.addHeader("Accept-Encoding", "gzip, deflate, br");
		httpPost.addHeader("Accept-Language", "zh-CN,zh;q=0.9");
		httpPost.addHeader("Connection", "keep-alive");
		httpPost.addHeader("Host", "i.emaotai.cn");
		httpPost.addHeader("Content-Type","application/x-www-form-urlencoded");
		httpPost.addHeader("Origin","https://www.emaotai.cn");
		httpPost.addHeader("Referer",referer);
		httpPost.addHeader("channelCode","01");
		httpPost.addHeader("terminalType","a1");
		httpPost.addHeader("appId", "1");
		httpPost.addHeader("channelId", "01");
		httpPost.addHeader("tenantId", "1");
		httpPost.addHeader("Flag", "1");
		httpPost.addHeader(
				"User-Agent",
				"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36");
		if(mwc.getAuth()!=null && !"".equals(mwc.getAuth())){
			httpPost.addHeader("auth",mwc.getAuth());
			String timestamp = System.currentTimeMillis()+"";
			String sign = JSUtil.excuFunctionA(mwc.getAuth()+timestamp);
			httpPost.addHeader("Sign", sign);
			httpPost.addHeader("Timestamp", timestamp);
		}
//		String cookie = "";
//		if(mwc.getAcw_tc()!=null && !"".equals(mwc.getAcw_tc())){
//			cookie += "acw_tc"+mwc.getAcw_tc()+";";
//		}
//		if(mwc.getCsrfToken()!=null && !"".equals(mwc.getCsrfToken())){
//			cookie += "csrfToken"+mwc.getCsrfToken()+";";
//		}
//		if(mwc.getAuth()!=null && !"".equals(mwc.getAuth())){
//			cookie += "sso.login.account.auth="+mwc.getAuth()+";";
//		}
//		if(mwc.getKcross()!=null && !"".equals(mwc.getKcross())){
//			cookie += "sensorsdata2015jssdkcross="+mwc.getKcross()+";";
//		}
//		if( !"".equals(cookie)){
//			httpPost.addHeader("Cookie",cookie);
//		}
		return httpPost;
	}
	private HttpPut setHttpPutHeader(HttpPut httpPut, MaotaiWebCookie mwc ,String referer) throws Exception {
		httpPut.addHeader("Accept","application/json, text/javascript, */*; q=0.01");
		httpPut.addHeader("Accept-Encoding", "gzip, deflate, br");
		httpPut.addHeader("Accept-Language", "zh-CN,zh;q=0.9");
		httpPut.addHeader("Connection", "keep-alive");
		httpPut.addHeader("Host", "i.emaotai.cn");
		httpPut.addHeader("Content-Type","application/x-www-form-urlencoded");
		httpPut.addHeader("Origin","https://www.emaotai.cn");
		httpPut.addHeader("Referer",referer);
		httpPut.addHeader("channelCode","01");
		httpPut.addHeader("terminalType","a1");
		httpPut.addHeader("appId", "1");
		httpPut.addHeader("channelId", "01");
		httpPut.addHeader("tenantId", "1");
		httpPut.addHeader("Flag", "1");
		httpPut.addHeader(
				"User-Agent",
				"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36");
		if(mwc.getAuth()!=null && !"".equals(mwc.getAuth())){
			httpPut.addHeader("auth",mwc.getAuth());
			String timestamp = System.currentTimeMillis()+"";
			String sign = JSUtil.excuFunctionA(mwc.getAuth()+timestamp);
			httpPut.addHeader("Sign", sign);
			httpPut.addHeader("Timestamp", timestamp);
		}
//		String cookie = "";
//		if(mwc.getAcw_tc()!=null && !"".equals(mwc.getAcw_tc())){
//			cookie += "acw_tc"+mwc.getAcw_tc()+";";
//		}
//		if(mwc.getCsrfToken()!=null && !"".equals(mwc.getCsrfToken())){
//			cookie += "csrfToken"+mwc.getCsrfToken()+";";
//		}
//		if(mwc.getAuth()!=null && !"".equals(mwc.getAuth())){
//			cookie += "sso.login.account.auth="+mwc.getAuth()+";";
//		}
//		if(mwc.getKcross()!=null && !"".equals(mwc.getKcross())){
//			cookie += "sensorsdata2015jssdkcross="+mwc.getKcross()+";";
//		}
//		if( !"".equals(cookie)){
//			httpPost.addHeader("Cookie",cookie);
//		}
		return httpPut;
	}
	public MaotaiWebCookie sendHttpPutProxy(String httpUrl,MaotaiWebCookie mwc, Map<String, String> maps,RequestConfig requestConfig, String referer) throws Exception {  
        HttpPut httpPost = new HttpPut(httpUrl);
        httpPost = setHttpPutHeader(httpPost,mwc,referer);
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();  
        for (String key : maps.keySet()) {  
            nameValuePairs.add(new BasicNameValuePair(key, maps.get(key)));  
        }  
        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));  
        return sendHttpPutProxy(httpPost,mwc,requestConfig);  
    }
	
	public MaotaiWebCookie sendHttpPostAppProxy(String httpUrl,MaotaiWebCookie mwc,String postData,RequestConfig requestConfig) throws Exception {  
        HttpPost httpPost = new HttpPost(httpUrl);
        httpPost = setHttpPostAppHeader(httpPost,mwc);
        httpPost.setEntity(new StringEntity(postData)); 
        return sendHttpPostAppProxy(httpPost,mwc,requestConfig);  
    }
    public MaotaiWebCookie sendHttpPostProxy(String httpUrl,MaotaiWebCookie mwc, Map<String, String> maps,RequestConfig requestConfig, String referer) throws Exception {  
        HttpPost httpPost = new HttpPost(httpUrl);
        httpPost = setHttpPostHeader(httpPost,mwc,referer);
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();  
        for (String key : maps.keySet()) {  
            nameValuePairs.add(new BasicNameValuePair(key, maps.get(key)));  
        }  
        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));  
        return sendHttpPostProxy(httpPost,mwc,requestConfig);  
    }
    public MaotaiWebCookie sendHttpPostLoginProxy(String httpUrl,MaotaiWebCookie mwc, Map<String, String> maps,RequestConfig requestConfig, String referer) throws Exception {  
        HttpPost httpPost = new HttpPost(httpUrl);
        httpPost = setHttpPostLoginHeader(httpPost,mwc,referer);
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();  
        for (String key : maps.keySet()) {  
            nameValuePairs.add(new BasicNameValuePair(key, maps.get(key)));  
        }  
        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));  
        return sendHttpPostProxy(httpPost,mwc,requestConfig);  
    }
    public MaotaiWebCookie sendHttpOptionProxy(String httpUrl,MaotaiWebCookie mwc,RequestConfig requestConfig, String referer) throws Exception {  
    	HttpOptions httpOptions = new HttpOptions(httpUrl);
    	httpOptions = setHttpOptionsHeader(httpOptions,mwc,referer);
        return sendHttpOptionsProxy(httpOptions,mwc,requestConfig);  
    }
    
    private MaotaiWebCookie sendHttpOptionsProxy(HttpOptions httpPost,MaotaiWebCookie mwc,RequestConfig config) throws Exception{  
        CloseableHttpClient httpClient = null;  
        httpClient = HttpClients.createDefault(); 
        httpPost.setConfig(config);  
        httpClient.execute(httpPost); 
        return mwc;  
    } 
    
    private MaotaiWebCookie sendHttpPostAppProxy(HttpPost httpPost,MaotaiWebCookie mwc,RequestConfig config) throws Exception{  
    	HttpClient httpClient = null;  
        HttpResponse response = null;  
        HttpEntity entity = null;
		String return_str = null;
        httpClient = HttpClients.createDefault(); 
        httpPost.setConfig(config);  
        response = httpClient.execute(httpPost); 
        entity = response.getEntity();
		return_str = EntityUtils.toString(entity, "UTF-8");
		System.out.println(return_str);
		mwc.setContent(return_str);
        return mwc;  
    } 
    private MaotaiWebCookie sendHttpPostProxy(HttpPost httpPost,MaotaiWebCookie mwc,RequestConfig config) throws Exception{  
        CloseableHttpClient httpClient = null;  
        CloseableHttpResponse response = null;  
        HttpEntity entity = null;
		String return_str = null;
        httpClient = HttpClients.createDefault(); 
        httpPost.setConfig(config);  
        response = httpClient.execute(httpPost); 
        Header[] headers = response.getAllHeaders();
        entity = response.getEntity();
		return_str = EntityUtils.toString(entity, "UTF-8");
		mwc.setContent(return_str);
		//System.out.println(return_str);
        if(headers!=null&&headers.length>0){
        	for (Header hea : headers) {
//            		System.out.println(hea.getName() + "::::::::::::::"
//							+ hea.getValue());
				if(hea.getName().contains("auth")){
					mwc.setAuth(hea.getValue());
				}
				if(hea.getName().contains("Set-Cookie")){
					String temp = hea.getValue();
					if(temp.contains("acw_tc")){
						mwc.setAcw_tc(temp.split(";")[0].split("=")[1]);
					}else if(temp.contains("csrfToken")){
						mwc.setCsrfToken(temp.split(";")[0].split("=")[1]);
					}
				}
			}
        }
        if (response != null) {  
            response.close();  
        }  
        if (httpClient != null) {  
            httpClient.close();  
        }  
        return mwc;  
    } 
    private MaotaiWebCookie sendHttpPutProxy(HttpPut httpPost,MaotaiWebCookie mwc,RequestConfig config) throws Exception{  
        CloseableHttpClient httpClient = null;  
        CloseableHttpResponse response = null;  
        HttpEntity entity = null;
		String return_str = null;
        httpClient = HttpClients.createDefault(); 
        httpPost.setConfig(config);  
        response = httpClient.execute(httpPost); 
        Header[] headers = response.getAllHeaders();
        entity = response.getEntity();
		return_str = EntityUtils.toString(entity, "UTF-8");
		mwc.setContent(return_str);
		//System.out.println(return_str);
        if(headers!=null&&headers.length>0){
        	for (Header hea : headers) {
//            		System.out.println(hea.getName() + "::::::::::::::"
//							+ hea.getValue());
				if(hea.getName().contains("auth")){
					mwc.setAuth(hea.getValue());
				}
				if(hea.getName().contains("Set-Cookie")){
					String temp = hea.getValue();
					if(temp.contains("acw_tc")){
						mwc.setAcw_tc(temp.split(";")[0].split("=")[1]);
					}else if(temp.contains("csrfToken")){
						mwc.setCsrfToken(temp.split(";")[0].split("=")[1]);
					}
				}
			}
        }
        if (response != null) {  
            response.close();  
        }  
        if (httpClient != null) {  
            httpClient.close();  
        }  
        return mwc;  
    } 

	public static JSONObject getAddress(String address) throws Exception{
    	
    	HttpGet httpGet = null;// 创建get请求
    	HttpClient httpClient = null;
		HttpResponse response = null;
		HttpEntity entity = null;
		String responseContent = null;
		JSONObject retObj = null;
		
		// 创建默认的httpClient实例.
//		httpClient = new DefaultHttpClient();
		httpClient = HttpClients.createDefault();
		// 执行请求
		boolean isOut = false;
		while(true){
			if(isOut)break;
			String httpUrl = "https://restapi.amap.com/v3/assistant/inputtips?s=rsv3&key=462f27e00614a30baa9ed1864455213f&city=&callback=jsonp_932577_&platform=JS&logversion=2.0&sdkversion=1.4.0&appname=https://www.cmaotai.com/ysh5/page/PersonalCenter/Address/addAddress.html?datas=1&csid=4573AA39-D2F9-4942-9B68-12BD116F03E9&keywords="+address+CustomName.getRandomChar();
//		    	if(address.contains("来安县"))httpUrl = "https://restapi.amap.com/v3/assistant/inputtips?s=rsv3&key=462f27e00614a30baa9ed1864455213f&city=&callback=jsonp_932577_&platform=JS&logversion=2.0&sdkversion=1.4.0&appname=https://www.cmaotai.com/ysh5/page/PersonalCenter/Address/addAddress.html?datas=1&csid=4573AA39-D2F9-4942-9B68-12BD116F03E9&keywords="+address+"汊河"+CustomName.getRandomChar();
	    	httpGet = new HttpGet(httpUrl);
	    	
	    	response = httpClient.execute(httpGet);
			entity = response.getEntity();  
			responseContent = EntityUtils.toString(entity, "UTF-8");  
			System.out.println(responseContent);
			responseContent = responseContent.replace("jsonp_932577_(", "");
			responseContent = responseContent.substring(0, responseContent.length()-1);
			
			JSONObject myObject = JSONObject.parseObject(responseContent);
			JSONArray tips = myObject.getJSONArray("tips");
			
			if(tips!=null && tips.size()>0){
				
				for(int i=0;i<tips.size();i++){
					JSONObject tipsi = tips.getJSONObject(i);
					System.out.println(tipsi);
					String district = tipsi.getString("district");
					if(tipsi.getString("address")!=null&&!"".equals(tipsi.getString("address"))&&!"[]".equals(tipsi.getString("address"))){
						System.out.println(district+"--"+tipsi.getString("name")+"--"+tipsi.getString("location")+"--"+tipsi.getString("address"));
						if(!tipsi.getString("name").contains("公交")&&!tipsi.getString("name").contains("地铁")&&tipsi.getString("address").length()>6) {
							retObj = tips.getJSONObject(i);
							isOut = true;
							break;
						}
					}
				}
			}
		}
		
		return retObj;
	}
}